/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.util;

import java.io.Serializable;



/**
*A class representing an one point equilibrium solution.
*
*@author Fonseca, Flavio & Pitangueira, Roque
*@version 1.0
*@since September 2003
*/

public class LinearEquationSystems implements Serializable{
    
	private static final long serialVersionUID = 1L;
    
    /** The IVector X of <i>A*X=B</i>.*/
    private IVector x;
    
    /** The IMatrix A of <i>A*X=B</i>.*/
    private IMatrix a;
    
    /** The IVector B of <i>A*X=B</i>.*/
    private IVector b;
	
 
    /** Returns the A IMatrix of this Solution.
    *@return The A IMatrix of this Solution.
    */
    public IMatrix getAMatrix() {
        return (a);
    };
    
    /** Sets the A IMatrix of this Solution.
    *@param aux The A IMatrix of this Solution.
    */
    public void setAMatrix(IMatrix aux){
        a = aux;
    };
    
 
    
    /** Returns the B IVector of this Solution.
    *@return The B IVector of this Solution.
    */
    public IVector getBVector(){
        return b;
    };
    
    /** Sets the B IVector of this Solution.
    *@param aux The B IVector of this Solution.
    */
    public void setBVector(IVector aux){
        b = aux;
    };
    
    /** Adds the given IVector to the B IVector of this Solution.<br>
    * B = B + a
    *@param a The IVector to be added to the B IVector of this Solution.
    */
    public void addToBVector(IVector a) {
        b.add(a);
    };
    
    
    /** Subtracts the given IVector from the B IVector of this Solution.<br>
    * B = B - a
    *@param a The IVector to be subtracted from the B IVector of this Solution.
    */
    public void subFromBVector(IVector a) {
        b.sub(a);
    };
    
 
    
    /** Returns the X IVector of this Solution.
    *@return The X IVector of this Solution.
    */
    public IVector getXVector() {
        return(x);
    };
    
    /** Sets the X IVector of this Solution.
    *@param aux The X IVector of this Solution.
    */
    public void setXVector(IVector aux) {
        x = aux;
    };
    
    /** Solves the system for the variable X.<br>
	*Solves the linear equations system <i>A*X=B</i><br>
	*A and B must have been previously set.
	*/
	public void solverX() throws SingularMatrixException
	{
		x = new IVector(a.getNumRow());
		IMatrix cho = new IMatrix(a.getNumRow(), a.getNumCol());
		IVector permutation = new IVector(a.getNumRow());
		a.LUD(cho,permutation);
		x.LUDBackSolve(cho,b,permutation);
//		a.choleskyD(cho);
//		x.choleskyDBackSolve(cho, b);
	};
	
 
	
	/** Solves the system for the variable B.<br>
	*Calculates the the product A*X=B<br>
	*A and X must have been previously set.
	*/
	public void solverB()
	{
		b.setSize(a.getNumRow());
		b.mul(a, x);
	};
	

	/** Solves the system for the variable X.<br>
    *Solves the linear equations system <i>M*X=V</i><br>
     */
    public IVector solverX(IMatrix m, IVector v) throws SingularMatrixException
    {
        x = new IVector(m.getNumRow());
        IMatrix cho = new IMatrix(m.getNumRow(), m.getNumCol());
        IVector permutation = new IVector(m.getNumRow());
		m.LUD(cho,permutation);
		x.LUDBackSolve(cho,v,permutation);
//        m.choleskyD(cho);
//        x.choleskyDBackSolve(cho, v);
        return x;
    };
    
 
}
